<template>
  <div class="reset-password">
    <div class="g-title">
      <span class="left">忘记密码</span>
      <p class="right"><span class="go-login" @click="goLogin">登陆</span> / <span class="go-login" @click="goRegister">注册</span></p>
    </div>
    <div class="reset-password-body">
      <div class="g-form">
        <el-form :model="resetForm" :rules="resetRules" ref="resetForm">
          <el-form-item label="手机号" prop="reciever">
            <el-input v-model="resetForm.reciever" placeholder="手机号"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="14">
              <el-form-item label="验证码" prop="ver_code">
                <el-input v-model="resetForm.ver_code" placeholder="请输入验证码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10" class="btnBox-wrap">
              <div @click="inputCode" :class="['btn', wait ? 'disabledBtn' : '']">{{ codeMessage }}</div>
            </el-col>
          </el-row>
          <el-form-item label="新密码" prop="new_password">
            <el-input v-model="resetForm.new_password" placeholder="新密码"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input show-password v-model="resetForm.confirmPassword" placeholder="请再次输入密码"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="line"></div>
      <div class="btn-next" @click="next('resetForm')">下一步</div>
    </div>
  </div>
</template>

<script>
import { validPhone } from '@/utils/validate'
export default {
  data() {
    const validateReciever = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      }
      if (!validPhone(value)) {
        callback(new Error('您输入的手机号格式不正确'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请确认密码'))
      }
      console.log(value, this.resetForm.password)
      if (value != this.resetForm.password) {
        callback(new Error('两次输入密码不一致，请重新输入'))
        this.resetForm.confirmPassword = ''
      } else {
        callback()
      }
    }
    return {
      resetForm: {
        reciever: '',
        new_password: '',
        ver_code: '',
        confirmPassword: ''
      },
      codeMessage: '获取验证码',
      wait: false,
      timer: null,
      resetRules: {
        reciever: [
          { required: true, trigger: 'blur', validator: validateReciever },
        ],
        ver_code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { max: 6, min: 6, message: '验证码长度为6', trigger: 'blur' }
        ],
        new_password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    goLogin() {
      // 登陆窗口统一由vuex控制
      this.$store.commit('user/SET_LOGIN', true)
    },
    goRegister() {
      this.$router.push({ path: '/register' })
      // 登陆窗口统一由vuex控制
      this.$store.commit('user/SET_LOGIN', false)
    },
    // 获取短信验证码
    inputCode() {
      this.$refs['resetForm'].validateField('reciever', valid => {
        if (!valid) {
          this.getRegisterCode()
          let time = 60
          this.wait = true
          this.timer = this.codeMessage = time + 's后重新发送'
          setInterval(() => {
            time--
            this.codeMessage = time + 's后重新发送'
            if (time <= 0) {
              this.codeMessage = '获取验证码'
              clearInterval(this.timer)
            }
          }, 1000);
        }
      })
    },
    getRegisterCode() {
      const params = {
        op: "send_register_ver_code",
        reciever: this.resetForm.reciever
      }
      this.$api.getRegisterCode({ ...params }).then(res => {
        if (res.status == '$SUCCESS') {
          this.$message.success('成功发送短信，请注意查收')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    next(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
          this.$store.dispatch('user/login', this.passwordForm)
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reset-password {
  max-width: 1368px;
  margin: 0 auto;
  padding: 60px 0 0;

  .g-title {
    margin-bottom: 40px;
    .left {
      font-size: 30px;
      font-family: Source Han Serif SC;
      font-weight: 800;
      color: #000000;
      line-height: 30px;
    }

    .right {
      float: right;
      font-size: 16px;
      color: #333;

      .go-login {
        color: #AA9884;
        cursor: pointer;
      }
    }
  }

  .reset-password-body {
    background: #fff;
    padding: 60px 208px 60px 208px;
  }

  .g-form {
    width: 582px;
    position: relative;
  }

  .line {
    width: 100%;
    height: 1px;
    background: #E8E8E8;
    transform: scaleY(0.8);
    margin: 40px 0;
  }

  .btn {
    width: 220px;
    height: 48px;
    background: #E0CBB4;
    color: #333;
    line-height: 48px;
    text-align: center;
    margin: 0 auto;
    cursor: pointer;
    font-size: 16px;

    &.el-button {
      line-height: 22px;
      display: block;
      margin: 0 auto;
      border-radius: 0;
    }
  }

  .disabledBtn {
    pointer-events: none;
  }

  .btnBox-wrap {
    position: absolute;
    right: 0;
    bottom: 22px;
  }

  .btn-next {
    width: 220px;
    height: 48px;
    margin: 0 auto;
    background: #E0CBB4;
    line-height: 48px;
    text-align: center;
    color: #333;
    font-size: 16px;
    cursor: pointer;
  }

  ::v-deep(.el-input__inner) {
    background-color: #f3f4f4;
    border-radius: 0;
    height: 50px;
  }

  ::v-deep(input::-webkit-input-placeholder) {
    color: #999;
  }

  ::v-deep(.el-form-item__content) {
    line-height: normal;
  }
}
</style>